package domain;

import java.text.NumberFormat;
import java.util.StringTokenizer;
import java.util.Locale;

import misc.Env;
import misc.ShellEnv;

public class Share {
	
	private String m_Name;
	private String m_Username;
	private String m_Password;
	private String m_PhysicalPath;
	private boolean m_Mounted;
	
	// Available memory in MB
	private double m_AvailableMemory;
	
	private double m_ReservedMemory;
	
	public Share() {}

	public Share(String name) {
		m_Name = name;
	}
	
	public String getAvailableMemory() {
		NumberFormat numberFormat;
		numberFormat = NumberFormat.getNumberInstance(Locale.GERMANY);
		numberFormat.setMaximumFractionDigits(2);
		
		return numberFormat.format(m_AvailableMemory);
	}
	
	public double getAvailableMemoryAsDouble() {
		return m_AvailableMemory;
	}
	
	public String getReservedMemory() {
		NumberFormat numberFormat;
		numberFormat = NumberFormat.getNumberInstance(Locale.GERMANY);
		numberFormat.setMaximumFractionDigits(2);
		
		return numberFormat.format(m_ReservedMemory);
	}
	
	public double getReservedMemoryAsDouble() {		
		return m_ReservedMemory;
	}
	
	public String getName() {
		return m_Name;
	}
	
	public void setName(String name) {
		m_Name = name;
	}
	
	public String getUsername() {
		return m_Username;
	}

	public void setUsername(String username) {
		m_Username = username;
	}

	public String getPassword() {
		return m_Password;
	}

	public void setPassword(String password) {
		m_Password = password;
	}
		
	public boolean isMounted() {
		return m_Mounted;
	}
	
	public void setMounted (boolean mounted) {
		m_Mounted = mounted;
	}
	
	public void removeMountPoint(String mountPoint, String hostname) {
		Env env = new ShellEnv();
		System.out.println(env.exec("rmdir " + mountPoint + "/" + hostname + "/" + m_Name, true));
	}
	
	public boolean mountShare(String mountPoint, String hostname) {
		
		String command, response;
		StringBuffer temp;
		Env env = new ShellEnv();
		
		/*
		 * Check whether client directory exists on host
		 * in order to create a directory as mount point.
		 */
		System.out.println(
				env.exec("if ! -d " + mountPoint + "/" + hostname + ";then\n" +
						" mkdir " + mountPoint + "/" + hostname + "\n" +
						" fi", true));
		
		// Create directory for mountpoint
		System.out.println(
				env.exec("mkdir " + mountPoint + "/" + hostname + "/" + m_Name.toString(), 
						true));
		
		if ((m_Username == null) || (m_Password == null)) {
			command = "smbmount //" + hostname + "/" + m_Name + 
			" " + mountPoint + "/" + hostname + "/" + m_Name +
			" -o guest";
			
		} else {
			
			if (m_Username.equals("")) {
				command = "smbmount //" + hostname + "/" + m_Name + 
				" " + mountPoint + "/" + hostname + "/" + m_Name +
				" -o guest";
				
			} else {
				command = "smbmount //" + hostname + "/" + m_Name + 
				" " + mountPoint + "/" + hostname + "/" + m_Name +
				" -o username=" + m_Username + "%" + m_Password;
			}
		}
		
		// first try to unmount mounting point
		unmountShare(mountPoint, hostname);
		
		// mount
		response = env.exec(command, true);
		System.out.println(response);
		
		if (response.contains("failed")) {
			m_Mounted = false;
			
		} else {
			m_Mounted = true;
			
			// Construct mount point.
			temp = new StringBuffer();
			temp.append("/" + mountPoint);
			temp.append("/" + hostname);
			temp.append("/" + m_Name);
			m_PhysicalPath = temp.toString();
			
			loadReservedMemory();
			loadAvailableMemory();
			
			return true;
		}
		
		return false;
	}
	
	public void unmountShare(String mountPoint, String hostname) {
		Env env = new ShellEnv();
		System.out.println(env.exec("smbumount " + mountPoint + "/" + hostname + "/" + m_Name, true));
	}
	
	public void loadAvailableMemory() {
		
		int i;
		String temp;
		StringTokenizer stringTokenizer;
		Env env = new ShellEnv();
		
		if (m_Mounted && !m_PhysicalPath.equals("")) {
			stringTokenizer = new StringTokenizer(env.exec("df " + m_PhysicalPath + " | grep " + m_Name, true));
			
			i=0;
			while (stringTokenizer.hasMoreTokens() && i<4) {
				temp = stringTokenizer.nextToken();
				
				switch (i) {
				
					case 3:
						m_AvailableMemory = Float.parseFloat(temp)/1024/1024;
						break;
				}
				i++;
			}
		}
	}
	
	public void loadReservedMemory() {
		
		int i;
		String temp;
		StringTokenizer stringTokenizer;
		Env env = new ShellEnv();
		
		if (m_Mounted && !m_PhysicalPath.equals("")) {
			stringTokenizer = new StringTokenizer(env.exec("du -s " + m_PhysicalPath + " | grep " + m_Name, true));
			
			i=0;
			while (stringTokenizer.hasMoreTokens() && i<4) {
				temp = stringTokenizer.nextToken();
				
				switch (i) {
				
					case 0:
						m_ReservedMemory = Float.parseFloat(temp)/1024/1024;
						break;
				}
				i++;
			}
		}
	}
	
}
